<?php
require "common.php";


class titleList{
    private $common="";
    public function __construct()
    {
        $this->common=new Common();
    }

    public  function getOneTitle(){
        $sql="SELECT * FROM `lunwen_yuanfile` WHERE user_info_id='".$_SESSION['id']."' and d0=1 and is_del=0";
        $query=mysqli_query($this->common->startMysql(),$sql);

        $info_arr=mysqli_fetch_all($query,MYSQLI_ASSOC);
        if ($info_arr){
            Common::rs($info_arr);
        }else{
            Common::rf("无数据");
        }
    }

    public  function getTwoTitle($one=''){
        if (empty($one))  Common::rf("无父标题id");
        $sql="SELECT * FROM `lunwen_yuanfile` WHERE user_info_id='".$_SESSION['id']."' and d0=2 and is_del=0 and d1='".$one."'";
        $query=mysqli_query($this->common->startMysql(),$sql);

        $info_arr=mysqli_fetch_all($query,MYSQLI_ASSOC);
        if ($info_arr){
            Common::rs($info_arr);
        }else{
            Common::rf("无数据");
        }

    }

    public function getThreeTitle($str=''){
        if (empty($str)) Common::rf("无id");
        $d_arr=explode("_",$str);
        if (empty($d_arr[0])||empty($d_arr[1])) Common::rf("d参数异常");
        $sql="SELECT * FROM `lunwen_yuanfile` WHERE user_info_id='".$_SESSION['id']."' and d0=3  and is_del=0 and d1='".$d_arr[0]."' and d2='".$d_arr[1]."'";
        $query=mysqli_query($this->common->startMysql(),$sql);
        $info_arr=mysqli_fetch_all($query,MYSQLI_ASSOC);
        if ($info_arr){
            Common::rs($info_arr);
        }else{
            Common::rf("无数据");
        }

    }

    public function getConnent($id=''){
        if (!$id) Common::rf("id为空");
        $sql="SELECT * FROM `lunwen_yuanfile` WHERE id=".$id;
        $query=mysqli_query($this->common->startMysql(),$sql);
        $info_arr=mysqli_fetch_array($query,MYSQLI_ASSOC);
        if ($info_arr){
            Common::rs($info_arr);
        }else{
            Common::rf("id不存在");
        }
    }


    public function getOneTitleName($d1){
        $sql="SELECT * FROM `lunwen_yuanfile` WHERE user_info_id='".$_SESSION['id']."' and d0=1 and is_del=0 and d1='".$d1."'";
        $query=mysqli_query($this->common->startMysql(),$sql);
        $info_arr=mysqli_fetch_array($query,MYSQLI_ASSOC);
        if ($info_arr){
            Common::rs($info_arr);
        }else{
            Common::rf("无数据");
        }
    }

    public function getTwoTitleName($str){
        if (empty($str)) Common::rf("无id");
        $d_arr=explode("_",$str);
        if (empty($d_arr[0])||empty($d_arr[1])) Common::rf("d参数异常");
        $sql="SELECT * FROM `lunwen_yuanfile` WHERE user_info_id='".$_SESSION['id']."' and d0=2 and is_del=0 and d1='".$d_arr[0]."' and d2='".$d_arr[1]."'";
        $query=mysqli_query($this->common->startMysql(),$sql);
        $info_arr=mysqli_fetch_array($query,MYSQLI_ASSOC);
        if ($info_arr){
            Common::rs($info_arr);
        }else{
            Common::rf("无数据");
        }
    }

    public function allTitle(){
        $show_arr=[];
        $sql="SELECT * FROM lunwen_yuanfile WHERE user_info_id='".$_SESSION['id']."' and is_del=0 and d0!=0 ORDER BY d1 asc ,d2 asc , d3 asc;";
        $query=mysqli_query($this->common->startMysql(),$sql);
        $info_arr=mysqli_fetch_all($query,MYSQLI_ASSOC);
        foreach ($info_arr as $k=>$v){
            $show_arr[$k]=$v;
            $show_arr[$k]["d"]="";
            if ($v['d1']){
                $show_arr[$k]["d"].=$v['d1'];
                if ($v['d2']){
                    $show_arr[$k]["d"].=".".$v['d2'];
                    $show_arr[$k]["正文标题"]='&nbsp;'.$show_arr[$k]["正文标题"];
                    if ($v['d3']){
                        $show_arr[$k]["d"].=".".$v['d3'];
                        $show_arr[$k]["正文标题"]='&nbsp;&nbsp;'.$show_arr[$k]["正文标题"];
                    }
                }
            }

        }
        if ($show_arr){
            Common::rs($show_arr);
        }else{
            Common::rf("无数据");
        }
    }
}

if (!empty($_GET["titleType"])){
    $select=$_GET["titleType"];
    switch ($select){
        case "1": $class_titlelist=new titleList(); $class_titlelist->getOneTitle();break;
        case "2": $class_titlelist=new titleList(); $class_titlelist->getTwoTitle($_GET['d']);break;
        case "3": $class_titlelist=new titleList(); $class_titlelist->getThreeTitle($_GET['d']);break;
        case "d1":$class_titlelist=new titleList(); $class_titlelist->getOneTitleName($_GET['d']);break;
        case "d2":$class_titlelist=new titleList(); $class_titlelist->getTwoTitleName($_GET['d']);break;
        case "d3":$class_titlelist=new titleList(); $class_titlelist->getThreeTitleName($_GET['d']);break;
        case "id":
            if (empty($_GET['id'])) Common::rf("id参数缺失");
            $class_titlelist=new titleList(); $class_titlelist->getConnent($_GET['id']);break;
        case "allTitle":$class_titlelist=new titleList(); $class_titlelist->allTitle();break;
        default:Common::rf("参数异常") ;
    }

}


